package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mvetc.dao.CommentDao;
import com.mvetc.dao.MovieDao;
import com.mvetc.entity.Comment;
import com.mvetc.entity.Country;
import com.mvetc.entity.Movie;
import com.mvetc.entity.PageInfo;
import com.mvetc.util.DBUtil;

public class MovieDaoImpl implements MovieDao {

	private CommentDao commentDao = new CommentDaoImpl();
	@Override
	public PageInfo<Movie> findBy(Map<String, String> params) {
		int pageNum = Integer.parseInt(params.get("pageNum"));
		int pageSize = Integer.parseInt(params.get("pageSize"));
		List<Movie> list = new ArrayList<>();
		ResultSet doQuery = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select A.movie_id")
		.append(",A.movie_name")
		.append(",A.movie_publish_date")
		.append(",A.movie_intro")
		.append(",A.movie_link")
		.append(",A.movie_picture")
		.append(",A.movie_rating")
		.append(",A.movie_type")
		.append(",A.movie_count")
		.append(",A.movie_duration")
		.append(",A.movie_status")
		.append(",A.movie_spell")
		.append(",A.movie_actors")
		.append(",B.country_id")
		.append(",B.country_name")
		.append(" from movie_info A inner join country_info B")
		.append(" on A.country_id = B.country_id");
		if ("".equals(params.get("movieNameKey"))) {
			sb.append(" order by A.movie_publish_date limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(), (pageNum-1)*pageSize,pageSize);
		}
		if (!"".equals(params.get("movieNameKey"))) {
			sb.append(" where A.movie_name like ? order by A.movie_publish_date limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(), "%" + params.get("movieNameKey") + "%", (pageNum-1)*pageSize,pageSize);
		}
		try {
			while(doQuery.next()) {
				String movieId = doQuery.getString("movie_id");
				
				Map<String, String> map2 = new HashMap<>();
				map2.put("movieId", movieId);
				map2.put("userNameKey", "");
				map2.put("movieNameKey", "");
				map2.put("pageNum", params.get("pageNum"));
				map2.put("pageSize", params.get("pageSize"));
				List<Comment> commentList = commentDao.findBy(map2).getData();
				
				String movieName = doQuery.getString("movie_name");
				String moviePublishDate = doQuery.getString("movie_publish_date");
				String movieIntro = doQuery.getString("movie_intro");
				String movieLink = doQuery.getString("movie_link");
				String moviePicture = doQuery.getString("movie_picture");
				String movieRating = doQuery.getString("movie_rating");
				String movieType = doQuery.getString("movie_type");
				String movieCount = doQuery.getString("movie_count");
				String movieDuration = doQuery.getString("movie_duration");
				String movieStatus = doQuery.getString("movie_status");
				String movieSpell = doQuery.getString("movie_spell");
				String movieActors = doQuery.getString("movie_actors");
				String countryId = doQuery.getString("country_id");
				String countryName = doQuery.getString("country_name");
				Country country = new Country(countryId, countryName, null);
				Movie movie = new Movie(movieId, movieName, moviePublishDate, movieIntro, movieLink, moviePicture, movieRating, movieCount, 
										movieDuration, movieStatus, movieSpell, movieType, movieActors, country, commentList);
				list.add(movie);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		doQuery =null;
		if (!"".equals(params.get("movieNameKey"))) {
			doQuery = DBUtil.doQuery("select count(*) from movie_info where movie_name like ?","%"+params.get("movieNameKey")+"%");
		}else {
			doQuery = DBUtil.doQuery("select count(*) from movie_info");
		}
		
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		PageInfo<Movie> pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		
		return pageInfo;
	}

	@Override
	public int insert(Movie movie) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into movie_info")
		.append(" (movie_id")
		.append(",movie_name")
		.append(",movie_publish_date")
		.append(",movie_link")
		.append(",movie_picture")
		.append(",movie_country_id")
		.append(",movie_count")
		.append(",movie_duration")
		.append(",movie_status")
		.append(",movie_spell)")
		.append(" values")
		.append(" (?,?,?,?,?,?,?,?,?,?)");
		int doUpdate = DBUtil.doUpdate(sb.toString(), movie.getMovieId(),movie.getMovieName(),movie.getMoviePublishDate(),
				movie.getMovieLink(),movie.getMoviePicture(),movie.getCountry().getCountryId(),movie.getMovieCount(),
				movie.getMovieDuration(),movie.getMovieStatus(),movie.getMovieSpell());
		return doUpdate;
	}

	@Override
	public int deleteById(String movieId) {
		int doUpdate = DBUtil.doUpdate("delete from movie_info where movie_id = ?", movieId);
		return doUpdate;
	}

	@Override
	public int updateMovie(Movie movie) {
		StringBuffer sb = new StringBuffer();
		sb.append("update movie_info")
		.append(" set movie_intro =?")
		.append(",movie_link =?")
		.append(",movie_picture =?")
		.append(",movie_status =?")
		.append(" where movie_id =?");
		int doUpdate = DBUtil.doUpdate(sb.toString(), movie.getMovieIntro(),movie.getMovieLink(),movie.getMoviePicture(),
				movie.getMovieStatus(),movie.getMovieId());
		return doUpdate;
	}

	/* @ SunZhiQiang
	 * @ see com.mvetc.dao.MovieDao#findmoviesInfo
	 * 实现查找电影信息的接口
	 */
	@Override
	public List<Map<String, Object>> findMoviesInfo(String[] showColumnsName, String[] selectColumnsName,
			String[] tableNames, String[] joinColumnsName,String...where) {
		List<Map<String, Object>> movieList = new ArrayList<>();
		   
		StringBuilder selectSql = DBUtil.selectSql(selectColumnsName, tableNames, joinColumnsName,where);
		ResultSet doQuery = DBUtil.doQuery(selectSql.toString());
		Map<String, Object> movieMap = null;
		try {
			while (doQuery.next()) {
				movieMap = new HashMap<>();
				for (int i = 0; i < selectColumnsName.length; i++) {
					movieMap.put(showColumnsName[i], doQuery.getObject(selectColumnsName[i]));
				}
				movieList.add(movieMap);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return movieList;
	}

	@Override
	public int insertInfo(String tableName,String[] columns,String[] values) {
		StringBuilder insertSql = DBUtil.insertSql(tableName, columns, values);
		int doUpdate = DBUtil.doUpdate(insertSql.toString());
		return doUpdate;
	}

	@Override
	public int updateInfo(String tableName, Map<String, String> set, Map<String, String> where) {
		StringBuilder updateSql = DBUtil.updateSql(tableName, set, where);
		int doUpdate = DBUtil.doUpdate(updateSql.toString());
		return doUpdate;
	}
	
	@Override
	public int deleteInfo(String tableName, Map<String, String> where) {
		StringBuilder deleteSql = DBUtil.deleteSql(tableName, where);
		int doUpdate = DBUtil.doUpdate(deleteSql.toString());
		return doUpdate;
	}
	
}
